﻿from django.db import models
from mptt.models import MPTTModel
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify


class ItemType(MPTTModel):
    name = models.CharField(max_length=200)
    slug = models.SlugField(editable=False)
    parent = models.ForeignKey('self',null=True, blank=True, related_name='children')
    
    def __unicode__(self):
        return self.name
        
    def save(self):
        self.slug = slugify(self.name)
        super(ItemType, self).save()

class Item(models.Model):
    type = models.ForeignKey('ItemType')    
    quantityType = models.ForeignKey('QuantityType')
    quantity = models.DecimalField(max_digits=5, decimal_places=2)
    
    def __unicode__(self):
        return self.type.name        
        
class QuantityType(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(editable=False)            
    
    def __unicode__(self):
        return self.name      
    
    def save(self):
        self.slug = slugify(self.name)
        super(QuantityType, self).save()
        
class Activity(MPTTModel):
    name = models.CharField(max_length=200)
    slug = models.SlugField(editable=False)
    parent = models.ForeignKey('self',null=True, blank=True, related_name='children')

    def __unicode__(self):
        return self.name

    def save(self):
        self.slug = slugify(self.name)
        super(Activity, self).save()        
    
    class meta:
        verbose_name_plural = 'Activities'

class Step(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(editable=False)
    activity = models.ForeignKey('Activity')
    tool = models.ForeignKey('Tool')
    items = models.ManyToManyField(Item)
    
    def __unicode__(self):
        return self.name        
    
    def save(self):
        self.slug = slugify(self.name)
        super(Step, self).save()    

        
class Tool(MPTTModel):
    name = models.CharField(max_length=200)
    slug = models.SlugField(editable=False)
    parent = models.ForeignKey('self',null=True, blank=True, related_name='children')
    
    def __unicode__(self):
        return self.name
    
    def save(self):
        self.slug = slugify(self.name)
        super(Tool, self).save()
        
class Recipe(MPTTModel):
    name = models.CharField(max_length=200)
    slug = models.SlugField(editable=False)
    parent = models.ForeignKey('self',null=True, blank=True, related_name='children')
    steps = models.ManyToManyField(Step)

    def __unicode__(self):
        return self.name
    
    def save(self):
        self.slug = slugify(self.name)
        super(Recipe, self).save()
